﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Forest.Lion.Domain.Services;
using Forest.Lion.Domain.Contracts;
using Microsoft.Practices.ServiceLocation;
using Forest.Lion.Web.Models;

namespace Forest.Lion.Web.Controllers {
    public class DepartmentController : AuthorizedController {
        public DepartmentController(IUserServices userServices,
          IServiceLocator serviceLocator)
            : base(userServices, serviceLocator) {
        }

        public ActionResult List() {
            var departments = Using<DepartmentHandler>().GetDeparmentsForCompany(CurrentUserId);

            return View(departments);
        }

        public ActionResult Edit(Guid Id) {
            var dept = Using<DepartmentHandler>().GetDepartment(Id);

            var formModel = new DepartmentFormModel {
                Id = dept.Id,
                DepartmentName = dept.DepartmentName,
                Mobile = dept.Mobile,
                Contact = dept.Contact,
                Fax = dept.Fax,
                Email = dept.Email,
                Tel = dept.Tel
            };

            return View(formModel);
        }

        [HttpPost]
        public ActionResult Edit(DepartmentFormModel formModel) {
            if (ModelState.IsValid) {
                Using<DepartmentHandler>().Update(formModel, CurrentUserId);
            }

            return RedirectToAction("List");
        }

        [HttpPost]
        public ActionResult Add(DepartmentFormModel formModel) {
            if (ModelState.IsValid) {
                Using<DepartmentHandler>().Create(formModel,CurrentUserId);

                return RedirectToAction("List");
            }

            return View();
        }

        public ActionResult Add() {
            var formModel = new DepartmentFormModel();

            formModel.Id = Guid.NewGuid();
            return View(formModel);
        }

    }
}
